package com.karaoke.android.home.adapter

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import com.daimajia.swipe.SwipeLayout
import com.daimajia.swipe.implments.SwipeItemRecyclerMangerImpl
import com.daimajia.swipe.interfaces.SwipeAdapterInterface
import com.daimajia.swipe.interfaces.SwipeItemMangerInterface
import com.daimajia.swipe.util.Attributes
import com.h6ah4i.android.widget.advrecyclerview.draggable.DraggableItemAdapter
import com.h6ah4i.android.widget.advrecyclerview.draggable.ItemDraggableRange
import com.h6ah4i.android.widget.advrecyclerview.utils.AbstractDraggableItemViewHolder
import com.karaoke.android.data.domain.PlayList
import com.karaoke.android.data.domain.Song
import com.karaoke.android.data.onUI
import com.karaoke.android.home.R
import com.karaoke.android.home.databinding.MediaEndItemBinding
import org.simple.eventbus.EventBus
import org.simple.eventbus.Subscriber
import rx.Observable
import java.util.concurrent.TimeUnit

/**
 * Created by allen on 2017/4/26.
 */
class MediaEndAdapter(val items: List<Song>) : RecyclerView.Adapter<MediaEndAdapter.BindingViewHolder>(),
        DraggableItemAdapter<MediaEndAdapter.BindingViewHolder>,
        SwipeItemMangerInterface, SwipeAdapterInterface
{
    init
    {
        EventBus.getDefault().register(this)
    }

    @Subscriber
    fun onMediaListChanged(mediaList: PlayList)
    {
        mItemManger.closeAllItems()
        Observable.timer(350, TimeUnit.MILLISECONDS).onUI { notifyDataSetChanged() }
    }


    override fun onBindViewHolder(holder: MediaEndAdapter.BindingViewHolder, position: Int)
    {
        val item = items[position]
        holder.binding.item = item
        mItemManger.bindView(holder.itemView, position)
    }

    override fun getItemCount(): Int
    {
        return items.count()
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MediaEndAdapter.BindingViewHolder
    {
        return BindingViewHolder(MediaEndItemBinding.inflate(LayoutInflater.from(parent?.context)))
    }

    init
    {
        setHasStableIds(true)
    }

    override fun getItemId(position: Int): Long
    {
        return items[position].id.toLong()
    }

    override fun onCheckCanStartDrag(p0: MediaEndAdapter.BindingViewHolder?, p1: Int, p2: Int, p3: Int): Boolean
    {
        return true
    }


    override fun onGetItemDraggableRange(p0: MediaEndAdapter.BindingViewHolder?, p1: Int): ItemDraggableRange?
    {
        return null
    }

    override fun onMoveItem(p0: Int, p1: Int)
    {

    }

    override fun onCheckCanDrop(p0: Int, p1: Int): Boolean
    {
        return true
    }


    class BindingViewHolder(val binding: MediaEndItemBinding) : AbstractDraggableItemViewHolder(binding.root)


    var mItemManger = SwipeItemRecyclerMangerImpl(this)

    public interface OnLoadMoreListener
    {
        fun onLoadMore()
    }

    var onLoadMoreListener: OnLoadMoreListener? = null

    override fun closeAllExcept(layout: SwipeLayout?) = mItemManger.closeAllExcept(layout)

    override fun setMode(mode: Attributes.Mode?)
    {
        mItemManger.mode = mode
    }

    override fun closeAllItems()
    {
        mItemManger.closeAllItems()
    }

    override fun removeShownLayouts(layout: SwipeLayout?)
    {
        mItemManger.removeShownLayouts(layout)
    }

    override fun getOpenItems(): MutableList<Int>?
    {
        return mItemManger.openItems
    }

    override fun isOpen(position: Int): Boolean
    {
        return mItemManger.isOpen(position)
    }

    override fun openItem(position: Int)
    {
        mItemManger.openItem(position)
    }

    override fun getMode(): Attributes.Mode
    {
        return mItemManger.mode
    }

    override fun getOpenLayouts(): MutableList<SwipeLayout>?
    {
        return mItemManger.openLayouts
    }

    override fun closeItem(position: Int)
    {
        mItemManger.closeItem(position)
    }

    override fun getSwipeLayoutResourceId(position: Int): Int
    {
        return R.id.swipe
    }
}


